package com.bdzl.admin.controller.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.bdzl.ecs.util.FddClientBaseUtils;
import com.bdzl.pub.config.Constant;
import com.bdzl.pub.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.bdzl.pub.entity.data.FddCaEntDO;
import com.bdzl.admin.service.data.FddCaEntService;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;

/**
 * 法大大企业 CA 证书信息
 * 
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:57:43
 */
 
@Controller
@RequestMapping("/bdzl/fddCaEnt")
public class FddCaEntController extends BaseController {
	@Autowired
	private FddCaEntService fddCaEntService;
	
	@GetMapping()
	String FddCaEnt(){
	    return "bdzl/fddCaEnt/fddCaEnt";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<FddCaEntDO> fddCaEntList = fddCaEntService.list(query);
		int total = fddCaEntService.count(query);
		PageUtils pageUtils = new PageUtils(fddCaEntList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "bdzl/fddCaEnt/add";
	}

	@GetMapping("/edit/{pceId}")
	String edit(@PathVariable("pceId") Integer pceId,Model model){
		FddCaEntDO fddCaEnt = fddCaEntService.get(pceId);
		model.addAttribute("fddCaEnt", fddCaEnt);
	    return "bdzl/fddCaEnt/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestBody FddCaEntDO fddCaEnt){
		if(fddCaEntService.save(fddCaEnt)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@RequestBody FddCaEntDO fddCaEnt){
		fddCaEntService.update(fddCaEnt);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer pceId){
		if(fddCaEntService.remove(pceId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] pceIds){
		fddCaEntService.batchRemove(pceIds);
		return R.ok();
	}


	@ResponseBody
	@GetMapping("/Bylist")
	public List<FddCaEntDO> Bylist(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<FddCaEntDO> fddCaEntList = fddCaEntService.list(query);
		return fddCaEntList;
	}

	/**
	 *企业CA证书申请(免审核模式)
	 * @param customerName 企业名称
	 * @param email 电子邮箱 可选 可以为null
	 * @param mobile  手机号码
	 * @param organization   组织机构
	 * @param pceEntNo   营业执照编号
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/syncCompanyAuto")
	public String syncCompanyAuto(String customerName , String email ,String mobile,String organization,String pceEntNo){
		FddClientBaseUtils fddClientBaseUtils = new FddClientBaseUtils(Constant.FDD_APPID,Constant.FDD_APPSECRET,Constant.FDD_VERSION,Constant.FDD_CAURL);
		String response= fddClientBaseUtils.syncCompanyAuto(customerName,email,mobile,organization);
		HashMap<String,String> map = JSON.parseObject(response,HashMap.class);
		//获取企业认证编号
	    String pceCustomerId = map.get("customer_id");
		Map fddMap = new HashMap();
		fddMap.put("pceCustomerId",pceCustomerId);
		List<FddCaEntDO> fddCaEntDOList = fddCaEntService.list(fddMap);
		if(fddCaEntDOList==null||fddCaEntDOList.size()==0){
			FddCaEntDO fddCaEnt = new FddCaEntDO();
			fddCaEnt.setPceCustomerId(pceCustomerId);
			fddCaEnt.setPceEntNo(pceEntNo);
			fddCaEntService.save(fddCaEnt);
		}
		return response;
	}
	
}
